﻿using UnityEngine;
using UnityEngine.UI;

namespace LocalizationUtils.Example
{
    public class RegexHyperLinkTextExample : MonoBehaviour
    {
        [SerializeField] public Text _txtTitle;
        [SerializeField] public RegexHyperLinkUGUIText _txtContent;

        public string _url;

        private void Start()
        {
            ShowContent();
        }

        private void ShowContent()
        {
            var url = LocalizationUtils.HttpUtils.TimestampUrl(_url);
            LocalizationUtils.HttpUtils.GetTextAsync(url, (error, text) =>
                {
                    if (!string.IsNullOrEmpty(error))
                    {
                        // XLog.Error(error);
                        return;
                    }

                    if (string.IsNullOrEmpty(text))
                    {
                        // XLog.Error("text is empty");
                        return;
                    }

                    int index = text.IndexOf('\n');
                    string title = index != -1 ? text.Substring(0, index) : text;
                    string content = index != -1 ? text.Substring(index + 1) : string.Empty;
                    _txtTitle.text = title;
                    _txtContent.text = content;
                }
            );
        }
    }
}